package com.a214.yxz.yuejian013.Adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.a214.yxz.yuejian013.JavaBean.OrderBean;
import com.a214.yxz.yuejian013.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Oliver on 10/27/2017.
 */

public class OrderListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater inflater;
    private List<OrderBean> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public OrderListAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.user_order_list_item, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderBean bean = list.get(position);
        if (bean == null) return;
        final ViewHolder viewholder = (ViewHolder) holder;
        viewholder.orderid.setText(bean.getId());
        switch (Integer.valueOf(bean.getVstate())){
            case 0:viewholder.orderState.setText("未完成");break;
            case 1:viewholder.orderState.setText("已完成");break;
            case 3:viewholder.orderState.setText("申请退款");break;
            case 4:viewholder.orderState.setText("退款成功");break;
        }
        viewholder.orderName.setText(bean.getName());
        viewholder.orderPrice.setText("￥"+bean.getPrice());
        viewholder.orderTime.setText(bean.getUpdate_time());
        viewholder.orderTotalPrice.setText("￥"+bean.getPay());
        viewholder.order_pic.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.email));
        if (mOnItemClickListener != null) {
            viewholder.revoke_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = viewholder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(viewholder.itemView, position);
                }
            });
            viewholder.revoke_btn.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = viewholder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(viewholder.itemView, position);
                    return false;
                }
            });
        }
        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/" + bean.getThumb())
                .placeholder(R.drawable.email)
                .into(viewholder.order_pic);
    }

    public void setList(List<OrderBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView orderid,orderState,orderName,orderPrice,orderTime,orderTotalPrice,revoke_btn;
        ImageView order_pic;

        public ViewHolder(View itemView) {
            super(itemView);
            orderid= (TextView) itemView.findViewById(R.id.user_order_orderId);
            orderState= (TextView) itemView.findViewById(R.id.user_order_orderState);
            orderName= (TextView) itemView.findViewById(R.id.user_order_orderName);
            revoke_btn= (TextView) itemView.findViewById(R.id.user_order_revoke_btn);
            orderPrice= (TextView) itemView.findViewById(R.id.user_order_orderPrice);
            orderTime = (TextView) itemView.findViewById(R.id.user_order_orderTime);
            orderTotalPrice = (TextView) itemView.findViewById(R.id.user_order_orderTotalPrice);
            order_pic = (ImageView) itemView.findViewById(R.id.user_order_orderPic);
        }

    }
}
